package com.intelligent.ispc.client.core.modal;

import com.intelligent.ispc.core.dto.FileDataDto;

import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import java.util.List;

/**
 * Created by Peter on 2016/7/12.
 */
public class GlobalTopBarModal {

    private FileListModal fileListModal;

    public FileListModal initFileListModal(List<FileDataDto> files) {
        if (fileListModal != null) {
            return fileListModal;
        }

        fileListModal = new FileListModal();
        fileListModal.init(files);
        fileListModal.addListDataListener(new FileListDataListener());

        return fileListModal;
    }
    public FileListModal resetFileListModal(List<FileDataDto> files) {
        fileListModal = null;
        fileListModal = new FileListModal();
        fileListModal.init(files);
        fileListModal.addListDataListener(new FileListDataListener());
        return fileListModal;
    }

    public FileListModal getFileListModal() {
        if (fileListModal != null) {
            return fileListModal;
        }

        fileListModal = new FileListModal();
        return fileListModal;
    }

    private class FileListModal<T extends FileDataDto> extends AbstractListModel {
        private List<T> data;

        public void init(List<T> obj) {
            data = obj;
        }

        @Override
        public int getSize() {
            return data.size();
        }

        @Override
        public String getElementAt(int index) {
            if (data.get(index) == null) {
                return null;
            }

            return data.get(index).getName();
        }

        public void addElement(T obj) {
            data.add(obj);
        }
    }

    private class FileListDataListener implements ListDataListener {


        @Override
        public void intervalAdded(ListDataEvent e) {

        }

        @Override
        public void intervalRemoved(ListDataEvent e) {

        }

        @Override
        public void contentsChanged(ListDataEvent e) {
        }
    }
}
